<!DOCTYPE html>
<html lang=en

  >

  <head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5" />
    <meta name="description" content="Java多线程基础知识：提升程序并发性能的关键 多线程技术可以充分利用计算机的多核心处理器，实现程序的并发执行，提高系统的性能和响应速度。本文将介绍Java多线程的基础知识，帮助读者理解多线程的概念、实现和常见应用场景。  什么是多线程 多线程是指在一个程序中同时执行多个线程，每个线程都独立执行自己的任务。多线程可以实现任务的并行处理，提高程序的效率和响应能力。  简单来说，多线程就像是一个人同时">
<meta property="og:type" content="article">
<meta property="og:title" content="Java多线程-基础知识">
<meta property="og:url" content="https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/index.html">
<meta property="og:site_name" content="一个有温度的小流年">
<meta property="og:description" content="Java多线程基础知识：提升程序并发性能的关键 多线程技术可以充分利用计算机的多核心处理器，实现程序的并发执行，提高系统的性能和响应速度。本文将介绍Java多线程的基础知识，帮助读者理解多线程的概念、实现和常见应用场景。  什么是多线程 多线程是指在一个程序中同时执行多个线程，每个线程都独立执行自己的任务。多线程可以实现任务的并行处理，提高程序的效率和响应能力。  简单来说，多线程就像是一个人同时">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2023-09-04T09:31:58.000Z">
<meta property="article:modified_time" content="2023-09-04T11:12:50.012Z">
<meta property="article:author" content="小流年">
<meta name="twitter:card" content="summary">
    
    
      
        
          <link rel="shortcut icon" href="/mrl/images/favicon.ico">
        
      
      
        
          <link rel="icon" type="image/png" href="/mrl/images/favicon-192x192.png" sizes="192x192">
        
      
      
        
          <link rel="apple-touch-icon" sizes="180x180" href="/mrl/images/apple-touch-icon.png">
        
      
    
    <!-- title -->
    <title>Java多线程-基础知识</title>
    <!-- async scripts -->
    <!-- Google Analytics -->


    <!-- Umami Analytics -->


    <!-- styles -->
    
<link rel="stylesheet" href="/mrl/css/style.css">

    <!-- persian styles -->
    
    <!-- rss -->
    
    
	<!-- mathjax -->
	
<meta name="generator" content="Hexo 6.3.0"></head>


    <body class="max-width mx-auto px3 ltr">
      
        <div id="header-post">
  <a id="menu-icon" href="#" aria-label="Menu"><i class="fa-solid fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#" aria-label="Menu"><i class="fa-solid fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" aria-label="Top" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fa-solid fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
        <!--
       --><li><a href="/mrl/search/">搜索</a></li><!--
     --><!--
       --><li><a href="/mrl/">首页</a></li><!--
     --><!--
       --><li><a href="/mrl/categories/">个人</a></li><!--
     --><!--
       --><li><a href="/mrl/articles/">归档</a></li><!--
     --><!--
       --><li><a href="/mrl/product/">产品</a></li><!--
     -->
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" aria-label="Previous post" href="/mrl/2023/09/05/%E5%AF%B9SPA%E5%8D%95%E9%A1%B5%E9%9D%A2%E7%9A%84%E7%90%86%E8%A7%A3/"><i class="fa-solid fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" aria-label="Next post" href="/mrl/2023/09/04/%E5%AE%9E%E7%8E%B0%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E5%BC%8F/"><i class="fa-solid fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" aria-label="Back to top" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fa-solid fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" aria-label="مشاركة المنشور" href="#"><i class="fa-solid fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">Previous post</span>
      <span id="i-next" class="info" style="display:none;">Next post</span>
      <span id="i-top" class="info" style="display:none;">Back to top</span>
      <span id="i-share" class="info" style="display:none;">مشاركة المنشور</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.facebook.com/sharer.php?u=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://twitter.com/share?url=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/&text=Java多线程-基础知识"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.linkedin.com/shareArticle?url=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/&title=Java多线程-基础知识"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://pinterest.com/pin/create/bookmarklet/?url=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/&is_video=false&description=Java多线程-基础知识"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=Java多线程-基础知识&body=Check out this article: https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/"><i class="fa-solid fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://getpocket.com/save?url=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/&title=Java多线程-基础知识"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://reddit.com/submit?url=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/&title=Java多线程-基础知识"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.stumbleupon.com/submit?url=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/&title=Java多线程-基础知识"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://digg.com/submit?url=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/&title=Java多线程-基础知识"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.tumblr.com/share/link?url=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/&name=Java多线程-基础知识&description="><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://news.ycombinator.com/submitlink?u=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/&t=Java多线程-基础知识"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    
    
      <div id="toc">
        <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%EF%BC%9A%E6%8F%90%E5%8D%87%E7%A8%8B%E5%BA%8F%E5%B9%B6%E5%8F%91%E6%80%A7%E8%83%BD%E7%9A%84%E5%85%B3%E9%94%AE"><span class="toc-number">1.</span> <span class="toc-text">Java多线程基础知识：提升程序并发性能的关键</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E5%A4%9A%E7%BA%BF%E7%A8%8B"><span class="toc-number">2.</span> <span class="toc-text">什么是多线程</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F"><span class="toc-number">3.</span> <span class="toc-text">Java多线程的实现方式</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8%E7%BA%BF%E7%A8%8B"><span class="toc-number">4.</span> <span class="toc-text">启动线程</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BA%BF%E7%A8%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-number">5.</span> <span class="toc-text">线程生命周期</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E9%97%AE%E9%A2%98"><span class="toc-number">6.</span> <span class="toc-text">线程安全问题</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E5%9C%BA%E6%99%AF"><span class="toc-number">7.</span> <span class="toc-text">常见场景</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E4%BF%9D%E8%AF%81%E7%BA%BF%E7%A8%8B%E6%98%AF%E5%AE%89%E5%85%A8%E7%9A%84"><span class="toc-number">8.</span> <span class="toc-text">如何保证线程是安全的</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%AD%BB%E9%94%81%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88"><span class="toc-number">9.</span> <span class="toc-text">死锁解决方案</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8jdk%E5%86%85%E7%BD%AE%E5%B7%A5%E5%85%B7%E6%A3%80%E6%B5%8B%E6%AD%BB%E9%94%81"><span class="toc-number">10.</span> <span class="toc-text">使用jdk内置工具检测死锁</span></a></li></ol>
      </div>
    
  </span>
</div>

          
            <div class="content index py4 ">
              
                    <article class="post h-entry" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle p-name" itemprop="name headline">
        Java多线程-基础知识
    </h1>



    <div class="meta">
      <span class="author p-author h-card" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span class="p-name" itemprop="name">小流年</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2023-09-04T09:31:58.000Z" class="dt-published" itemprop="datePublished">2023-09-04</time>
        
      
    </div>


      
    <div class="article-category">
        <i class="fa-solid fa-archive"></i>
        <a class="category-link" href="/mrl/categories/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B/">Java多线程</a>
    </div>


      

    </div>
  </header>
  

  <div class="content e-content" itemprop="articleBody">
    <h1 id="Java多线程基础知识：提升程序并发性能的关键"><a href="#Java多线程基础知识：提升程序并发性能的关键" class="headerlink" title="Java多线程基础知识：提升程序并发性能的关键"></a>Java多线程基础知识：提升程序并发性能的关键</h1><ul>
<li>多线程技术可以充分利用计算机的多核心处理器，实现程序的并发执行，提高系统的性能和响应速度。本文将介绍Java多线程的基础知识，帮助读者理解多线程的概念、实现和常见应用场景。</li>
</ul>
<h1 id="什么是多线程"><a href="#什么是多线程" class="headerlink" title="什么是多线程"></a>什么是多线程</h1><ul>
<li><p>多线程是指在一个程序中同时执行多个线程，每个线程都独立执行自己的任务。多线程可以实现任务的并行处理，提高程序的效率和响应能力。</p>
</li>
<li><p>简单来说，多线程就像是一个人同时做多个任务。假设你是一个程序，有一个任务是打字，另一个任务是听音乐。如果你只有一个线程，那么你需要先完成一个任务，再去做另一个任务。但如果你有两个线程，你可以同时进行打字和听音乐，提高了效率。</p>
</li>
<li><p>Java多线程的好处包括：</p>
</li>
<li><p>提高程序的并发性和吞吐量，可以同时处理多个任务。</p>
</li>
<li><p>提高程序的响应性，可以在某个线程等待的时候执行其他线程的任务。</p>
</li>
<li><p>充分利用多核处理器的优势，提高程序的执行效率。</p>
</li>
<li><p>然而，多线程也需要注意一些问题，比如线程安全性、资源竞争等。在多线程编程中，需要合理地设计和管理线程，确保数据的一致性和线程之间的协作。</p>
</li>
</ul>
<h1 id="Java多线程的实现方式"><a href="#Java多线程的实现方式" class="headerlink" title="Java多线程的实现方式"></a>Java多线程的实现方式</h1><ul>
<li><p>方式一</p>
</li>
<li><p>继承Thread类重写run方法创建线程</p>
</li>
<li><p>Thread类本质上是实现了Runnable接口的一个实例，代表一个线程的实例。启动线程的唯一方法就是通过Thread类的start()实例方法。start()方法是一个native方法，它将启动一个新线程，并执行run()方法。这种方式实现多线程很简单，通过自己的类直接extend Thread，并复写run()方法，就可以启动新线程并执行自己定义的run()方法。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">public class MyThread extends Thread&#123;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public void run() &#123;</span><br><span class="line">        System.out.println(&quot;Thread&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</li>
<li><p>方式二</p>
</li>
<li><p>实现Runnable接口创建线程</p>
</li>
<li><p>如果自己的类已经extends另一个类，就无法直接extends Thread，此时，可以实现一个Runnable接口，如下：</p>
</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">public class RunnableThread implements Runnable&#123;</span><br><span class="line">   </span><br><span class="line">@Override</span><br><span class="line">public void run() &#123;</span><br><span class="line">    System.out.println(&quot;runnable&quot;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li>方式三</li>
<li>实现Callable接口通过FutureTask包装器来创建Thread线程：</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">public class CallableThread implements Callable &#123;</span><br><span class="line"></span><br><span class="line">@Override</span><br><span class="line">public Object call() throws Exception &#123;</span><br><span class="line">    return &quot;Callable&quot;;</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h1 id="启动线程"><a href="#启动线程" class="headerlink" title="启动线程"></a>启动线程</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">public static void main(String[] args) throws ExecutionException, InterruptedException &#123;</span><br><span class="line">   MyThread myThread = new MyThread();</span><br><span class="line">   myThread.start();</span><br><span class="line"></span><br><span class="line">   RunnableThread runnableThread = new RunnableThread();</span><br><span class="line">   new Thread(runnableThread).start();</span><br><span class="line"></span><br><span class="line">   CallableThread callableThread = new CallableThread();</span><br><span class="line">   </span><br><span class="line">   FutureTask oneTask = new FutureTask(callableThread);</span><br><span class="line">   new Thread(oneTask).start();</span><br><span class="line">   </span><br><span class="line">   System.out.println(oneTask.get());;</span><br><span class="line"></span><br><span class="line">   Lock lock = new ReentrantLock();</span><br><span class="line">   </span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h1 id="线程生命周期"><a href="#线程生命周期" class="headerlink" title="线程生命周期"></a>线程生命周期</h1><ul>
<li><p><strong>新建状态（New）</strong>：当线程对象对创建后，即进入了新建状态，如：Thread t &#x3D; new MyThread();</p>
</li>
<li><p><strong>就绪状态（Runnable）</strong>：当调用线程对象的start()方法（t.start();），线程即进入就绪状态。处于就绪状态的线程，只是说明此线程已经做好了准备，随时等待CPU调度执行，并不是说执行了t.start()此线程立即就会执行；</p>
</li>
<li><p><strong>运行状态（Running）</strong>：当CPU开始调度处于就绪状态的线程时，此时线程才得以真正执行，即进入到运行状态。注：就 绪状态是进入到运行状态的唯一入口，也就是说，线程要想进入运行状态执行，首先必须处于就绪状态中；</p>
</li>
<li><p><strong>阻塞状态（Blocked）</strong>：处于运行状态中的线程由于某种原因，暂时放弃对CPU的使用权，停止执行，此时进入阻塞状态，直到其进入到就绪状态，才 有机会再次被CPU调用以进入到运行状态。根据阻塞产生的原因不同，阻塞状态又可以分为三种：</p>
<pre><code>  1.等待阻塞：运行状态中的线程执行wait()方法，使本线程进入到等待阻塞状态；

  2.同步阻塞 – 线程在获取synchronized同步锁失败(因为锁被其它线程所占用)，它会进入同步阻塞状态；

  3.其他阻塞 – 通过调用线程的sleep()或join()或发出了I/O请求时，线程会进入到阻塞状态。当sleep()状态超时、join()等待线程终止或者超时、或者I/O处理完毕时，线程重新转入就绪状态。
</code></pre>
</li>
<li><p><strong>死亡状态（Dead）</strong>：线程执行完了或者因异常退出了run()方法，该线程结束生命周期</p>
</li>
</ul>
<h1 id="线程安全问题"><a href="#线程安全问题" class="headerlink" title="线程安全问题"></a>线程安全问题</h1><ul>
<li><strong>线程安全问题</strong>：当多个线程操作同一个数据时，可能会出现数据的异常。</li>
<li>具体解决线程安全问题就需要用到同步和互斥的概念了。</li>
<li>互斥：一个共享资源，A访问时，其它的都被阻塞（不能访问），当A访问完成时另一个才能访问；</li>
<li>同步：A的结果是B的前提，也就是A、B不能同时运行。</li>
<li>互斥是一种特殊的同步，而同步是更为复杂的互斥。</li>
</ul>
<h1 id="常见场景"><a href="#常见场景" class="headerlink" title="常见场景"></a>常见场景</h1><ul>
<li>多个线程同时操作一个共享资源的时候可能会出现业务安全问题，称为线程安全问题。</li>
<li>同时访问共享资源</li>
<li>存在修改共享资源</li>
</ul>
<h1 id="如何保证线程是安全的"><a href="#如何保证线程是安全的" class="headerlink" title="如何保证线程是安全的"></a>如何保证线程是安全的</h1><ul>
<li>让多个线程实现先后依次访问共享资源，这样就解决了安全问题。</li>
<li>就好比说，同时进入一个系统，到取钱的时候，排个队。</li>
</ul>
<h1 id="死锁解决方案"><a href="#死锁解决方案" class="headerlink" title="死锁解决方案"></a>死锁解决方案</h1><p>死锁解决方案<br>预防死锁的方式就是打破四个必要条件中的任意一个即可。</p>
<p>1）打破互斥条件：在系统里取消互斥。若资源不被一个进程独占使用，那么死锁是肯定不会发生的。但一般来说在所列的四个条件中，“互斥”条件是无法破坏的。因此，在死锁预防里主要是破坏其他几个必要条件，而不去涉及破坏“互斥”条件。。</p>
<p>2）打破请求和保持条件：采用资源预先分配策略，即进程运行前申请全部资源，满足则运行，不然就等待。</p>
<p>每个进程提出新的资源申请前，必须先释放它先前所占有的资源。</p>
<p>3）打破不可剥夺条件：当进程占有某些资源后又进一步申请其他资源而无法满足，则该进程必须释放它原来占有的资源。</p>
<p>4）打破环路等待条件：实现资源有序分配策略，将系统的所有资源统一编号，所有进程只能采用按序号递增的形式申请资源。</p>
<h1 id="使用jdk内置工具检测死锁"><a href="#使用jdk内置工具检测死锁" class="headerlink" title="使用jdk内置工具检测死锁"></a>使用jdk内置工具检测死锁</h1><ul>
<li>方法一 jvisualvm.exe</li>
<li>进入你的jdk安装路径中，打开jdk&#x2F;bin&#x2F;jvisualvm.exe</li>
<li>优点：比较细，比较全面</li>
<li>缺点：加载有点慢！</li>
</ul>

  </div>
</article>



                      
                        <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
        
          <li><a href="/mrl/search/">搜索</a></li>
        
          <li><a href="/mrl/">首页</a></li>
        
          <li><a href="/mrl/categories/">个人</a></li>
        
          <li><a href="/mrl/articles/">归档</a></li>
        
          <li><a href="/mrl/product/">产品</a></li>
        
      </ul>
    </div>

    
    
      <div id="toc-footer" style="display: none">
        <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%EF%BC%9A%E6%8F%90%E5%8D%87%E7%A8%8B%E5%BA%8F%E5%B9%B6%E5%8F%91%E6%80%A7%E8%83%BD%E7%9A%84%E5%85%B3%E9%94%AE"><span class="toc-number">1.</span> <span class="toc-text">Java多线程基础知识：提升程序并发性能的关键</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E5%A4%9A%E7%BA%BF%E7%A8%8B"><span class="toc-number">2.</span> <span class="toc-text">什么是多线程</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F"><span class="toc-number">3.</span> <span class="toc-text">Java多线程的实现方式</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8%E7%BA%BF%E7%A8%8B"><span class="toc-number">4.</span> <span class="toc-text">启动线程</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BA%BF%E7%A8%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-number">5.</span> <span class="toc-text">线程生命周期</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E9%97%AE%E9%A2%98"><span class="toc-number">6.</span> <span class="toc-text">线程安全问题</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E5%9C%BA%E6%99%AF"><span class="toc-number">7.</span> <span class="toc-text">常见场景</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E4%BF%9D%E8%AF%81%E7%BA%BF%E7%A8%8B%E6%98%AF%E5%AE%89%E5%85%A8%E7%9A%84"><span class="toc-number">8.</span> <span class="toc-text">如何保证线程是安全的</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%AD%BB%E9%94%81%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88"><span class="toc-number">9.</span> <span class="toc-text">死锁解决方案</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8jdk%E5%86%85%E7%BD%AE%E5%B7%A5%E5%85%B7%E6%A3%80%E6%B5%8B%E6%AD%BB%E9%94%81"><span class="toc-number">10.</span> <span class="toc-text">使用jdk内置工具检测死锁</span></a></li></ol>
      </div>
    

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.facebook.com/sharer.php?u=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://twitter.com/share?url=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/&text=Java多线程-基础知识"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.linkedin.com/shareArticle?url=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/&title=Java多线程-基础知识"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://pinterest.com/pin/create/bookmarklet/?url=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/&is_video=false&description=Java多线程-基础知识"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=Java多线程-基础知识&body=Check out this article: https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/"><i class="fa-solid fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://getpocket.com/save?url=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/&title=Java多线程-基础知识"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://reddit.com/submit?url=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/&title=Java多线程-基础知识"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.stumbleupon.com/submit?url=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/&title=Java多线程-基础知识"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://digg.com/submit?url=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/&title=Java多线程-基础知识"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.tumblr.com/share/link?url=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/&name=Java多线程-基础知识&description="><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://news.ycombinator.com/submitlink?u=https://liushuaijun.gitee.io/mrl/2023/09/04/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B%E2%80%93%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/&t=Java多线程-基础知识"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fa-solid fa-bars fa-lg" aria-hidden="true"></i> Menu</a>
        
          <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fa-solid fa-list fa-lg" aria-hidden="true"></i> TOC</a>
        
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fa-solid fa-share-alt fa-lg" aria-hidden="true"></i> Share</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fa-solid fa-chevron-up fa-lg" aria-hidden="true"></i> Top</a>
    </div>

  </div>
</div>

                          
                            <footer id="footer">
  <div class="footer-left">
    Copyright &copy;
    
    
    2020-2023
    小流年
  </div>
  <div class="footer-right">
    <nav>
      <ul>
        <!--
       --><li><a href="/mrl/search/">搜索</a></li><!--
     --><!--
       --><li><a href="/mrl/">首页</a></li><!--
     --><!--
       --><li><a href="/mrl/categories/">个人</a></li><!--
     --><!--
       --><li><a href="/mrl/articles/">归档</a></li><!--
     --><!--
       --><li><a href="/mrl/product/">产品</a></li><!--
     -->
      </ul>
    </nav>
  </div>
  
  <div class="busuanzi">
    
      <!-- 不蒜子统计 -->
      <span id="busuanzi_container_site_pv">
        网站总访问量:<span id="busuanzi_value_site_pv"></span>
      </span>
      
     <span class="post-meta-divider">|</span>
      <span id="busuanzi_container_site_uv" style='display:none'>
          本站访客数<span id="busuanzi_value_site_uv"></span>人
      </span>
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
     
  </div>
</footer>

            </div>
            <!-- styles -->



  <link rel="preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" crossorigin="anonymous" onload="this.onload=null;this.rel='stylesheet'"/>


              <!-- jquery -->

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" crossorigin="anonymous"></script>




<!-- clipboard -->

  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.7/clipboard.min.js" crossorigin="anonymous"></script>
  
  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"Copy to clipboard!\">";
    btn += '<i class="fa-regular fa-clone"></i>';
    btn += '</span>';
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "Copied!");
      e.clearSelection();
    })
  })
  </script>


<script src="/mrl/js/main.js"></script>

<!-- search -->

<!-- Baidu Analytics -->

<!-- Cloudflare Analytics -->

<!-- Disqus Comments -->

<!-- utterances Comments -->

    </body>

    </html>